package com.example.service;


import com.example.dao.FoodDao;
import com.example.entities.Food;
import com.example.server_engineer.service.AbstractTypedService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

//import javax.annotation.Resource;
import java.util.List;


/**
 * @author Administrator
 */
@Slf4j
@Service
public class FoodService
        extends AbstractTypedService<Food,Integer> {

    FoodDao foodDao;

    FoodService(FoodDao dao){
        this.dataContext = dao;
        this.foodDao = dao;
    }

//    @Resource
//    FileStorageService fileStorageService;

    public List<Food> queryAll(){
        List<Food> lst = null;
        try {
            //
            lst = ((FoodDao) this.dataContext).findAll();
            log.info("find food: " + lst.size());

        } catch (Exception e) {
            log.warn(e.toString());
            //throw new RuntimeException(e);
        }
        return lst;
    }

    // 根据商家信息查看食物
    public List<Food> queryFoodByBusinessId(int businessId){
        List<Food> foodList = null;
        try{
            foodList = foodDao.findFoodByBusinessId(businessId);
            log.info("foodlist size : "+ foodList.size());
        }catch(Exception e){
            log.warn(e.toString());
        }
        return foodList;
    }

    // 根据食物名称查找食物
    public Food queryFoodByName(String name){
        Food food = null;
        try{
            food = foodDao.findFoodByName(name);
//            log.info("foodlist size : "+ foodList.size());
        }catch(Exception e){
            log.warn(e.toString());
        }
        return food;
    }

    // 根据食物名称查询商家id
    public Integer findBusinessIdByName(String name){
        Integer businessId = null;
        try{
            businessId = foodDao.findBusinessIdByName(name);
        }catch(Exception e){
            log.warn(e.toString());
        }
        return businessId;
    }

//    public String setImgByFoodId(Integer id, MultipartFile file){
//        FileInfo upload = fileStorageService.of(file).upload();
//        Integer integer = foodDao.updateImgByFoodId(upload.getUrl(),id);
//
//        if(integer == 0){
//            return "false";
//        }
//        return "true";
//    }

    public Page<Food> queryFoodByPage(Pageable pageable){
        return (Page<Food>) foodDao.queryPage(pageable,null);
    }
}
